/*
 * Created on 2005-9-4
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.store;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.jedi.BaseObj;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author dai
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class ApplyMgr extends BaseObj {
	public ApplyMgr() {}
	
	public Apply get(String id) {
		clearErr();
		Apply apply = new Apply(id);
		DbConn conn = new DbConn();
		try {
			String sql = "";
			sql = "select a.*,b.service_id from t_storage_apply a left outer join t_contract_info b on a.contract_id = b.contract_id where a.list_id = ?";
			conn.prepare(sql);
			conn.setString(1,id);
			DbRs rs = conn.executeQuery();
			if (rs == null || rs.size() ==0) return null;
			apply.setStorageId(get(rs,0,"storage_id"));
			apply.setContractId(get(rs,0,"contract_id"));
			apply.setServiceId(get(rs,0,"service_id"));
			apply.setTaskId(get(rs,0,"task_id"));
			apply.setApplyDate(get(rs,0,"apply_date"));
			apply.setReturnDate(get(rs,0,"return_date"));
			apply.setCustomerName(get(rs,0,"customer_name"));
			apply.setAddr(get(rs,0,"addr"));
			apply.setEngineerId(get(rs,0,"engineer_id"));
			apply.setEngineerName(get(rs,0,"engineer_name"));
			apply.setApplyType(getInt(rs,0,"apply_type"));
			apply.setFixDate(get(rs,0,"fix_date"));
			apply.setFixedDate(get(rs,0,"fixed_date"));
			apply.setUpkeep(getLong(rs,0,"upkeep"));
			apply.setOrigin(get(rs,0,"origin"));
			apply.setStatus(getInt(rs,0,"status"));
			return apply;
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
	}
	
	public List get(String otherSQL,String orderField) {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select a.*,b.service_id from t_storage_apply a left outer join t_contract_info b on a.contract_id = b.contract_id where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals("")) 
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			if (getTopNum() > 0)
				sql += " limit " + getTopNum();
			conn.prepare(sql);
			rs = conn.executeQuery();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return null;
		}
		finally {
			conn.close();
		}
		if (rs != null && rs.size() >0) {
			List r = new ArrayList();
			for(int i=0;i<rs.size();i++) {
				Apply apply = new Apply(get(rs,i,"list_id"));
				apply.setStorageId(get(rs,i,"storage_id"));
				apply.setContractId(get(rs,i,"contract_id"));
				apply.setServiceId(get(rs,i,"service_id"));
				apply.setTaskId(get(rs,i,"task_id"));
				apply.setApplyDate(get(rs,i,"apply_date"));
				apply.setReturnDate(get(rs,i,"return_date"));
				apply.setCustomerName(get(rs,i,"customer_name"));
				apply.setAddr(get(rs,i,"addr"));
				apply.setEngineerId(get(rs,i,"engineer_id"));
				apply.setEngineerName(get(rs,i,"engineer_name"));
				apply.setApplyType(getInt(rs,i,"apply_type"));
				apply.setFixDate(get(rs,i,"fix_date"));
				apply.setFixedDate(get(rs,i,"fixed_date"));
				apply.setUpkeep(getLong(rs,i,"upkeep"));
				apply.setOrigin(get(rs,i,"origin"));
				apply.setStatus(getInt(rs,i,"status"));
				r.add(apply);
			}
			return r;
		}
		return null;
	}
	
	public List get(String otherSQL,String orderField,int pageNo, int pageSize) {
		clearErr();
		DbRs rs = null;
		DbConn conn = null;
		try {
			conn = new DbConn();
			String sqlCount = "select count(*) as total from t_storage_apply a left outer join t_contract_info b on a.contract_id = b.contract_id where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sqlCount += otherSQL;
			conn.prepare(sqlCount);
			DbRs rsCount = conn.executeQuery();
			int total = getInt(rsCount,0,"total");
			setTotal(total);
			boolean b = total % pageSize == 0;
			int pageCount = b ? (total / pageSize) : (total / pageSize + 1);
			if (pageNo > pageCount) pageNo = pageCount;
			String sql = "select a.*,b.service_id from t_storage_apply a left outer join t_contract_info b on a.contract_id = b.contract_id where 1=1";
			if (otherSQL != null && !otherSQL.trim().equals(""))
				sql += otherSQL;
			if (orderField != null && !orderField.trim().equals(""))
				sql += " order by " + orderField;
			conn.prepare(sql);
			rs = conn.executeQuery(pageNo, pageSize);
		} catch (SQLException ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
			return new ArrayList();
		} catch (Exception e) {
			e.printStackTrace();
			setErr(e.getMessage());
			return new ArrayList();
		} finally {
			conn.close();
		}
		List r = new ArrayList();
		if (rs == null || rs.size() <= 0)
			return r;
		for (int i = 0; i < rs.size(); i++) {
			Apply apply = new Apply(get(rs,i,"list_id"));
			apply.setStorageId(get(rs,i,"storage_id"));
			apply.setContractId(get(rs,i,"contract_id"));
			apply.setServiceId(get(rs,i,"service_id"));
			apply.setTaskId(get(rs,i,"task_id"));
			apply.setApplyDate(get(rs,i,"apply_date"));
			apply.setReturnDate(get(rs,i,"return_date"));
			apply.setCustomerName(get(rs,i,"customer_name"));
			apply.setAddr(get(rs,i,"addr"));
			apply.setEngineerId(get(rs,i,"engineer_id"));
			apply.setEngineerName(get(rs,i,"engineer_name"));
			apply.setApplyType(getInt(rs,i,"apply_type"));
			apply.setFixDate(get(rs,i,"fix_date"));
			apply.setFixedDate(get(rs,i,"fixed_date"));
			apply.setUpkeep(getLong(rs,i,"upkeep"));
			apply.setOrigin(get(rs,i,"origin"));
			apply.setStatus(getInt(rs,i,"status"));
			r.add(apply);
		}
		return r;
	}
}
